package de.hijacksoft.androidupdate;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import de.hijacksoft.wyc.R;

public class UpdateButton extends Button implements OnClickListener {

	String packageName = "";
	int versionCode = -1;

	public UpdateButton(Context context) {
		super(context);
		start();
	}

	public UpdateButton(Context context, AttributeSet attSet) {
		super(context, attSet);
		start();
	}

	public UpdateButton(Context context, AttributeSet attSet, int defStyle) {
		super(context, attSet, defStyle);
		start();
	}

	private void start() {
		try {
			versionCode = getContext().getPackageManager().getPackageInfo(
					getContext().getPackageName(), 0).versionCode;
			packageName = getContext().getPackageManager().getPackageInfo(
					getContext().getPackageName(), 0).packageName;
			new DownloadHelper() {

				public boolean readLine(String line) {
					String[] versionTexts = line.split("versionCode=\"");

					for (int i = 1; i < versionTexts.length; i++) {
						try {
							String versionText = versionTexts[i];
							String v = "";

							char c;
							do {
								c = versionText.charAt(0);
								versionText = versionText.replaceFirst("" + c,
										"");
								if (c >= '0' && c <= '9')
									v += c;
							} while (c != '"' && versionText.length() > 1);

							int version = Integer.parseInt(v);

							if (version > versionCode) {
								showUpdate();
							}
							return false;
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					return super.readLine(line);
				};
			}.download("http://cashtracker.googlecode.com/svn/trunk/Ausgaben/AndroidManifest.xml");
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		setEnabled(false);
		setVisibility(View.GONE);
		setOnClickListener(this);
	}

	private void showUpdate() {
		setText(getContext().getString(R.string.update));
		//question(getContext().getString(R.string.getupdate)); <- leads to bad window-management
		setEnabled(true);
		setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View arg0) {
		String APP_MARKET_URL = "market://details?id=" + packageName;
		Intent intent = new Intent(Intent.ACTION_VIEW,
				Uri.parse(APP_MARKET_URL));
		try {
			getContext().startActivity(intent);
		} catch (Exception e) {
			dialog(null, getContext().getString(R.string.nomarket));
		}
	}

	private final void question(String text) {
		AlertDialog alertDialog = new AlertDialog.Builder(getContext())
				.create();
		alertDialog.setMessage(text);
		alertDialog.setButton(getContext().getString(R.string.yes),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						UpdateButton.this.onClick(UpdateButton.this);
					}
				});
		alertDialog.setButton2(getContext().getString(R.string.no),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// nothing
					}
				});
		alertDialog.show();
	}
	
	private final void dialog(String title, String text){
		final AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
		if(title!=null)alertDialog.setTitle(title);
		alertDialog.setMessage(text);
		alertDialog.setCanceledOnTouchOutside(true);
		alertDialog.setButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				alertDialog.cancel();
			}
		});
		alertDialog.show();
	}

}
